import hashlib
import time
# 操作html界面的
from selenium.webdriver.common.by import By

from automate_testing.tool.DButils import DButils

class RegisterOperation:
    @staticmethod
    def  register(driver,username,password,repassword):
        # 输入用户名
        driver.find_element(By.XPATH,'//*[@id="username"]').send_keys(username)
        driver.find_element(By.XPATH,'//*[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH,'//*[@id="repassword"]').send_keys(repassword)
        driver.find_element(By.XPATH,'//*[@id="login_btn"]').click()

        time.sleep(2)
    @staticmethod
    def get_result(driver):
        username_prompt=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[1]/span[1]').text
        password_prompt=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[2]/span[1]').text
        repassword_prompt=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[3]/span[1]').text
        return username_prompt,password_prompt,repassword_prompt

    @staticmethod
    def success_sql(username):
        if username == 'wangwu':
            sql='delete from user where username=%s'
            DButils().update(sql,(username,))

    @staticmethod
    def failure_sql(username,password,prompt):
        if username == 'wangwu1' and '已存在' in prompt:
            insert_sql = "insert into user(username,password,status) values(%s,%s,0)"
            DButils().update(insert_sql, (username, hashlib.md5(password.encode('utf-8')).hexdigest()))
        if username == 'wangwu':
            sql='delete from user where username=%s'
            DButils().update(sql,(username,))
    @staticmethod
    def get_database_result(username):
        #  获取实际结果
        select_sql = "select  * from user  where username = %s"
        data = DButils().select(select_sql,(username,))
        return data

    @staticmethod
    def insert_user_sql(username,password,paypwd):
        delete_sql="delete from user where username=%s"
        DButils().update(delete_sql,(username,))
        insert_sql = "insert into user(username,password,paypwd,status) values(%s,%s,%s,0)"
        DButils().update(insert_sql,(username,hashlib.md5(password.encode('utf-8')).hexdigest(),paypwd))


"""
mysql> desc user;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int         | NO   | PRI | NULL    | auto_increment |
| username   | varchar(20) | YES  |     | NULL    |                |
| realname   | varchar(20) | YES  |     | NULL    |                |
| password   | varchar(40) | YES  |     | NULL    |                |
| IDcard     | varchar(18) | YES  |     | NULL    |                |
| phone      | varchar(20) | YES  |     | NULL    |                |
| email      | varchar(20) | YES  |     | NULL    |                |
| paypwd     | int         | YES  |     | NULL    |                |
| status     | int         | YES  |     | NULL    |                |
| reputation | varchar(20) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
10 rows in set (0.05 sec)
"""